package uit.com.project.iCampus.util;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import uit.com.project.iCampus.model.BuildingInfo;
import uit.com.project.iCampus.model.BuildingRate;
import uit.com.project.iCampus.model.BuildingTypeEnum;
import uit.com.project.iCampus.model.User;
import uit.com.project.iCampus.model.UserOnline;


public class XMLParserServerUtil {
	public static JAXBContext jaxbContext = initJAXBContext();
	public static Marshaller marshaller;
	public static StringWriter stringWriter;
	
	public static JAXBContext initJAXBContext(){
		try {
			return jaxbContext = JAXBContext.newInstance(ListObject.class,User.class, UserOnline.class, BuildingInfo.class, BuildingRate.class);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String marshalObject(Object obj){
		String result = "";
		if (obj == null)
			return "";
		else {
			try {
				marshaller = jaxbContext.createMarshaller();
				marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
				stringWriter = new StringWriter();
				marshaller.marshal(obj, stringWriter);
				result = stringWriter.toString();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public static String marshalListObject(ListObject<Object> list){
		String result = "";
		try {
			marshaller = jaxbContext.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			stringWriter = new StringWriter();
			marshaller.marshal(list, stringWriter);
			result = stringWriter.toString();
			stringWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@SuppressWarnings({ "unchecked", "unused", "rawtypes" })
	public static void main(String[] args) throws JAXBException {
		
		User user = new User("maixuanhiep", "123", "Mai Xuân Hiệp");
		UserOnline userOnline = new UserOnline(user, 124434, 23423);
		UserOnline userOnline2 = new UserOnline(user, 234354, 67867);
		
		BuildingInfo buildingInfo = new BuildingInfo(BuildingTypeEnum.FOODS, "KFC", 14, 345, "abc");
		BuildingRate buildRate1 = new BuildingRate(buildingInfo, 8, "note 8");
		BuildingRate buildRate2 = new BuildingRate(buildingInfo, 10, "note 10");
		
//		Set<BuildingRate> buidingRates = new HashSet<BuildingRate>();
//		buidingRates.add(buildRate1);
//		buidingRates.add(buildRate2);
//		buildingInfo.setBuidingRates(buidingRates);
//		
		List<User> list = new ArrayList<User>();
		list.add(user);
		list.add(user);
		ListObject<Object> listObj = new ListObject(list);
		
		List<BuildingRate> listRates = new ArrayList<BuildingRate>();
		listRates.add(buildRate1);
		listRates.add(buildRate2);
		ListObject<Object> listObjRates = new ListObject(listRates);
		
		List<BuildingInfo> buildingInfos = new ArrayList<BuildingInfo>();
		buildingInfos.add(buildingInfo);
		buildingInfos.add(buildingInfo);
		ListObject<Object> listObjBuildingInfos = new ListObject(buildingInfos);
		
		System.out.println(marshalListObject(listObjBuildingInfos));
//		System.out.println(marshalListObject(listObjRates));
//		System.out.println(marshalObject(buildingInfo));
//		System.out.println(marshalListObject(listObj));
//		System.out.println(marshalObject(user));
//		System.out.println(marshalObject(userOnline));
	}
}
